using System;
using System.Text;
using System.Web.UI.WebControls;
using eWorld.UI;
using KangaUtils;
using TimeSheetBLL;

namespace TimeSheet.Protected.View.Model
{
    /// <summary>
    ///		Summary description for EditStaff.
    /// </summary>
    public abstract class EditStaff : EntityControl
    {
        protected Button btnSave;
        protected ValidationSummary ValidationSummary1;
        protected TextBox Email;
        protected RegularExpressionValidator RegularExpressionValidator1;
        protected Label lblAddEditTitle;
        protected TextBox LastName;
        protected TextBox FirstName;
        protected CheckBox Active;
        protected RequiredFieldValidator RequiredFieldValidator2;
        protected RequiredFieldValidator RequiredFieldValidator1;
        protected RequiredFieldValidator RequiredFieldValidator3;
        protected CollapsablePanel cpEntity;

        private void Page_Load(object sender, EventArgs e)
        {
            BindControlToEntity();
            if (BoundEntity.ID > 0)
            {
                btnSave.Text = "Update";
                lblAddEditTitle.Text = "Edit Staff";
            }
            else
            {
                btnSave.Text = "Create";
                lblAddEditTitle.Text = "Add Staff";
            }
        }

        #region Web Form Designer generated code

        protected override void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        ///		Required method for Designer support - do not modify
        ///		the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
            this.Load += new System.EventHandler(this.Page_Load);
        }

        #endregion

        private void btnSave_Click(object sender, EventArgs e)
        {
            LoadEntityFromForm();
            bool sent = false;

            if (BoundEntity.ID <= 0)
            {
                User user = (User) BoundEntity;
                user.UserName = user.Email;
                user.Password =
                    SecurityUtils.GenerateRandomPassword(AppSettings.PasswordLength);
                MailUtils.SendEmail(
                    AppSettings.SmtpServer,
                    GetEmailSubject(),
                    GetEmailBody(user),
                    AppSettings.AdminEmail,
                    user.Email);
                sent = true;
            }
            SaveBoundEntity();
            if (sent)
            {
                Response.Redirect("~/Protected/Staff.aspx");
            }
        }

        private string GetEmailBody(User user)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(
                "<p>" + user.FullName +
                ", you have been registered as a Timesheet User. </p>");
            sb.Append(
                "<p>To log in, go to this location: http://" + AppSettings.SiteURL +
                " and enter in ");
            sb.Append("the following information: </p>");
            sb.Append("<p>E-Mail: " + user.Email + "<br />");
            sb.Append("Password: " + user.Password + "</p>");

            return sb.ToString();
        }

        private string GetEmailSubject()
        {
            return "Your Timesheet Password";
        }
    }
}